package org.gc.service.security;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.gc.common.entity.security.RoleGroup;
import org.gc.common.vo.security.RoleGroupVO;
import org.gc.common.vo.security.RoleVO;
import org.gc.common.vo.security.UserVO;
import org.gc.dao.security.RoleGroupDao;
import org.gc.service.base.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RoleGroupServiceImpl extends BaseServiceImpl<RoleGroup, RoleGroupVO, Long>  implements
		RoleGroupService {

	private RoleGroupDao roleGroupDao;
	private UserService userService;
	private RoleService roleService; 
	

	@Override
	public List<RoleGroupVO> getAll() {
		List<RoleGroupVO> list = super.getAll();
		Set<RoleGroupVO> set = new HashSet<RoleGroupVO>(list);
		list = new ArrayList<RoleGroupVO>(set);
		return list;
	}

	@Override
	public void remove(Long id) {
		
		RoleGroupVO vo = this.get(id);
		if(vo.getUsers()!=null && vo.getUsers().size()>0){
			throw new RuntimeException("用户组["+vo.getName()+"]中用户数目不为空，不能删除！");
		}
		
		super.remove(id);
	}

	@Override
	@PostConstruct
	public void afterConstruct() {
		super.setBaseDao(roleGroupDao);
		super.setEntityClass(RoleGroup.class);
		super.setEntityVOClass(RoleGroupVO.class);
	}

	
	@Override
	public Set<RoleVO> getAllRoles(Set<Long> roleGroupIds) {
		Set<RoleVO> set = new HashSet<RoleVO>();
		 
		if(roleGroupIds!=null){
			List<RoleGroupVO> roleGroupList;
			Set<RoleGroupVO> set2 = getAll(roleGroupIds);
			roleGroupList = new ArrayList<RoleGroupVO>(set2);
			
			for(RoleGroupVO rg : roleGroupList){
				set.addAll(rg.getRoles());
			}
		}
		return set;
	}

	
	@Override
	public Set<RoleGroupVO> getAll(Set<Long> roleGroupIds) {
		List<Long> ids = new ArrayList<Long>(roleGroupIds); 
		List<RoleGroupVO> roleGroupList = new ArrayList<RoleGroupVO>();
		for (Long id : ids) {
			RoleGroupVO vo = get(id);
			roleGroupList.add(vo);
		}
		
		Set<RoleGroupVO> set2 = new HashSet<RoleGroupVO>(roleGroupList);
		return set2;
	}

	@Override
	public void authorize(UserVO user, Set<Long> roleGroupIds) {
		
		Set<RoleVO> newRoles = new HashSet<RoleVO>();
		Set<RoleGroupVO> newRoleGroups = new HashSet<RoleGroupVO>();
		 
		
		if(roleGroupIds!=null){
			newRoles.addAll(this.getAllRoles(roleGroupIds));
			newRoleGroups = this.getAll(roleGroupIds);
		}
	
		if(newRoles.size()==0){
			newRoles.add(roleService.getRole("ROLE_USER"));
		}
		
		user.setRoles(newRoles);
		user.setRoleGroups(newRoleGroups);
		userService.save(user);
	}
	
	@Override
	public void update(RoleGroupVO roleGroup, Set<String> roles) {
		
		Set<RoleVO> newRoles = new HashSet<RoleVO>();
		if (roles != null) {
			for (String r : roles) {
				RoleVO role = roleService.getRole(r);
				if (role != null) {
					newRoles.add(role);
				}
			}
		}
		
		// 每个角色默认必须有2个权限：
		newRoles.add(roleService.getRole("ROLE_USER"));
		newRoles.add(roleService.getRole("ROLE_ADMIN"));

		roleGroup.setRoles(newRoles);
		roleGroup = super.save(roleGroup);
		
		// 更改拥有该roleGroup的所有users的权限
		Set<UserVO> users = roleGroup.getUsers();
		for(UserVO user : users){
			Set<RoleGroupVO> rgSet = user.getRoleGroups();
			Set<Long> roleGroupIds = new HashSet<Long>();
			for(RoleGroupVO rg : rgSet){
				roleGroupIds.add(rg.getId());
			}
			this.authorize(user, roleGroupIds);
		}
	}
	
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@Autowired
	public void setRoleGroupDao(RoleGroupDao roleGroupDao) {
		this.roleGroupDao = roleGroupDao;
	} 
	
	@Autowired
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
}
